package com.xhwl.centiotdata.service.maozhouhe.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.service.maozhouhe.IMaoZhouHeIocService;
import com.xhwl.common.pojo.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class MaoZhouHeIocServiceImpl implements IMaoZhouHeIocService {
    private static final String URL = "https://api.vika.cn/fusion/v1/datasheets/dstu9rnsuP5ntByPgB/records";
    private static final String AUTHORIZATION = "Bearer uskP2QOtxkQCsG3HjPPGw5m";

    @Override
    public ResultJson<Map<String, Object>> getMaoZhouHerecords(Map<String, Object> param) {
        log.info("req param:{}", JSON.toJSONString(param));
        String body = HttpUtil.createGet(URL)
                .form(param)
                .header("Authorization", AUTHORIZATION)
                .execute().body();
        JSONObject jsonObject = JSON.parseObject(body);
        if (200 == jsonObject.getInteger("code")) {
            return ResultJson.successWithResult(jsonObject.getJSONObject("data").toJavaObject(Map.class));
        } else {
            return ResultJson.fail(jsonObject.getString("message"));
        }
    }
}
